# coding: utf-8
# File   :  yaml_control
# Author :  wangsheng
# @Time  :  2025/5/28 下午1:23
import yaml
import os
from typing import Union, Dict, List
from utils.ptah_get import ensure_path_sep


class YamlReader:
    """安全高效的YAML文件操作工具类"""

    @staticmethod
    def read(file_path=ensure_path_sep("\\common\\config.yaml")) -> Union[Dict, List]:
        """
        安全读取YAML文件（自动处理编码和异常）

        :param file_path: YAML文件路径
        :return: 解析后的字典或列表
        :raises: FileNotFoundError, yaml.YAMLError
        """
        if not os.path.exists(file_path):
            raise FileNotFoundError(f"YAML文件不存在: {file_path}")

        with open(file_path, 'r', encoding='utf-8') as f:
            return yaml.safe_load(f)


if __name__ == '__main__':
    data = YamlReader.read()
    print(data.get("api_key"))
